// components/agora-player/agora-player.js
const Utils = require("../../utils/util.js")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    uid: {
      type: String,
      value: ""
    },
    url: {
      type: String,
      value: "",
      // observer: function (newVal, oldVal, changedPath) {
      //   // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
      //   // 通常 newVal 就是新设置的数据， oldVal 是旧数据
      //   Utils.log(`player url changed from ${oldVal} to ${newVal}, path: ${changedPath}`);
      // }
    },
    // enableCamera: {
    //   type: Boolean,
    //   value: true
    // },
    // 是否免提
    hasFree: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    playContext: null,
    detached: false,
    enableCamera: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * start live player via context
     * in most cases you should not call this manually in your page
     * as this will be automatically called in component ready method
     */
    start: function () {
      const uid = this.data.uid;
      // if (this.data.detached) {
      //   Utils.log(`try to start pusher while component already detached`);
      //   return;
      // }
      if(!this.data.detached) this.data.playContext.play();
    },

    /**
     * stop live pusher context
     */
    stop: function () {
      const uid = this.data.uid;
      Utils.log(`stopping player ${uid}`);
      this.data.playContext.stop();
    },

    rePlay: function() {
      this.data.playContext.play();
    },

    /**
     * rotate video by rotation
     */
    // rotate: function (rotation) {
    //   let orientation = rotation === 90 || rotation === 270 ? "horizontal" : "vertical";
    //   Utils.log(`rotation: ${rotation}, orientation: ${orientation}, uid: ${this.data.uid}`);
    //   this.setData({
    //     orientation: orientation
    //   });
    // },

    /**
     * 播放器状态更新回调
     */
    playerStateChange: function(e) {   
      let uid = parseInt(e.target.id.split("-")[1]);
      if (e.detail.code === 2004) {
        Utils.log(`live-player ${uid} started playing`);
        if(this.data.status === "loading") {
          this.setData({
            status: "ok"
          });
        }
      } else if (e.detail.code === -2301) {
        Utils.log(`live-player ${uid} stopped`, "error");
        this.setData({
          status: "error"
        })
      }
    },
    stateChange: function(e) {
      // console.log('播放状态发生变化', e.detail.code)
      // if(e.detail.code === 2105) {
      //   this.setData({ enableCamera: false })
      // } else {
      //   this.setData({ enableCamera: true })
      // }
    }
  },
  /**
   * 组件生命周期
   */
  ready: function () {
    this.data.playContext || (this.data.playContext = wx.createLivePlayerContext(`player-${this.data.uid}`, this));
    if(this.data.url) {
      // this.start();
      this.data.playContext.play();
    }
  },
  moved: function () {
  },
  detached: function () {
    this.data.playContext && this.data.playContext.stop();
    this.data.detached = true;
  }
})
